=pod
  Basic and rough substitutions for some Portable XAMPP server path and port settings, you could want it either after you moved the app folder or you have to change a port.

 Copyright 2015 Gabriel Czernikier



    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

sub readInsDir {
	print 'XAMPP\'s installation absolute path: ';
	$insdir = <STDIN>;

	$insdir =~ s|\\|/|g;  # Sorry for the non-MS-W$ users
	$insdir =~ s|/$||;  # the surround where it will be placed implies it cannot have trailing slash
	$insdir =~ s/\n$//;
}
sub readOldDir {
	print "Old/factory Default path (empty=defaults to /xampp): ";
    $oldir= <STDIN>;
    $oldir='/xampp' if ($oldir=~/^$/);

	$oldir =~ s|\\|/|g;  # Sorry for the non-MS-W$ users
	$oldir =~ s|/$||;  # the surround where it will be placed implies it cannot have trailing slash
	$oldir =~ s/\n$//;

}
sub checkVersion {
	open FH, '<', "$insdir/readme_en.txt" or die "can't open datafile: $!";
	$txt=<FH>;
	if($txt !~ /5\.6\.14/) {
	  $txt =~ /(Version\s*+[^\s]*)/;
	  print "Warning: tested for 5.6.14 but this is $1. Proceed [y/N]? ";
	  $ans =<STDIN>;
	  $ans =~ s/\n$//;
	  exit 0 unless $ans =~ /^y$/i;
	}
	close FH;
}

sub readPorts {
	print 'HTTP port: ';
	$httprt = <STDIN>;
    $httprt =~ s/\n$//;
	print 'SSL port: ';
	$sslprt = <STDIN>;
    $sslprt =~ s/\n$//;
}

sub readDry {
	print 'Run dry mode (will output to dry.log in current dir) (Y/n)?: ';
	$dry=<STDIN>;
}

sub readFile {
	open FH, '<', shift or die "can't open datafile: $!";
	my $txt=<FH>;
	close FH;
	return $txt;
}

sub writeFile {
	if ($dry =~ /^n$/i) {
	  open FH, '>:raw', shift or die "can't open datafile: $!";
	  print FH shift;
	  close FH;
	}
	else {
	  print STDERR 'Run dry mode for ' . shift . "\n";
      open FH, '>>', 'dry.log';
	  print FH shift;
	  close FH;
	}
}

sub subst4ListenSrvName {
	my $s = shift;
    my $p = shift;
	$$s =~ s/^((?:Listen|ServerName).*)\b\d+(.*)$/\1$p\2/gm;
}
sub subst4DocSrvRoot {
	my $s = shift;
	$$s =~ s|^((?:DocumentRoot\|ServerRoot\|LoadFile\|LoadModule php5_module)\s*")$oldir(.*)$|\1$insdir\2|gm;
}
sub subst4Directory {
	my $s = shift;
	$$s =~ s|^(<Directory\s*")$oldir(.*)$|\1$insdir\2|gm;
}
sub subst4VirtualHost {
	my $s = shift;
    my $p = shift;
	$$s =~ s|^(<VirtualHost.*)\b\d+(.*)$|\1$p\2|gm;
}
sub subst4Logs {
	my $s = shift;
	$$s =~ s|^((?:ErrorLog\|TransferLog\|CustomLog)\s*")$oldir(.*)$|\1$insdir\2|gm;
}

sub subst4MySQL {
	my $s = shift;
	$$s =~ s|^((?:socket\|basedir\|tmpdir\|datadir\|plugin_dir\|innodb_data_home_dir\|innodb_log_group_home_dir)\s*=\s*")$oldir(.*)$|\1$insdir\2|gm;
}

#clean dry.log
open FH, '>', 'dry.log'; close FH;
print "XAMPPconf - Basic and rough substitutions for some Portable XAMPP configurations.\n";
print "This program is distributed as is without any warranty.\n";
readInsDir;
readOldDir;
checkVersion;
readPorts;
readDry;
local $/=undef;

# httpd.conf
$txt=readFile "$insdir/apache/conf/httpd.conf";
subst4ListenSrvName \$txt, $httprt;
subst4DocSrvRoot \$txt, $insdir;
subst4Directory \$txt, $insdir;
writeFile "$insdir/apache/conf/httpd.conf", $txt;

# httpd-ssl.conf
$txt=readFile "$insdir/apache/conf/extra/httpd-ssl.conf";
subst4ListenSrvName \$txt, $sslprt;
subst4DocSrvRoot \$txt, $insdir;
subst4Directory \$txt, $insdir;
subst4VirtualHost \$txt, $sslprt;
subst4Logs \$txt, $insdir;
writeFile "$insdir/apache/conf/extra/httpd-ssl.conf", $txt;

# httpd-xampp.conf
$txt=readFile "$insdir/apache/conf/extra/httpd-xampp.conf";
subst4ListenSrvName \$txt, $httprt;
subst4DocSrvRoot \$txt, $insdir;
subst4Directory \$txt, $insdir;
writeFile "$insdir/apache/conf/extra/httpd-xampp.conf", $txt;

# my.ini
# There are other possible locations for this file but these take effect when calling mysqld from command line (those reported with mysqld --verbose --help).
$txt=readFile "$insdir/mysql/bin/my.ini";
subst4MySQL \$txt, $insdir;
writeFile "$insdir/mysql/bin/my.ini", $txt;

# xampp-control.ini
$txt=readFile "$insdir/xampp-control.ini";
# TODO if stanza doesn't exist to substitute, create it.
$txt =~ s/([ServicePorts][^[]*Apache=).*$/\1$httprt/ms;
$txt =~ s/([ServicePorts][^[]*ApacheSSL=).*$/\1$sslprt/ms;
writeFile "$insdir/xampp-control.ini", $txt;

